/*
 * @Author: shanghongyun 2879455630@qq.com
 * @Date: 2025-05-30 15:56:30
 * @LastEditors: shanghongyun 2879455630@qq.com
 * @LastEditTime: 2025-07-14 17:18:26
 * @FilePath: \A05\Drive\battery.c
 * @Description: 电池管理
 */
#include "battery.h"
#include "systick.h"


void dis_charge_L(void){
    DIS_CHARGE_L();
}

void en_charge_L(void){
    EN_CHARGE_L();
}

void dis_charge_R(void){
    DIS_CHARGE_R();
}

void en_charge_R(void){
    EN_CHARGE_R();
}


void pwm_off_L(void){
    EPWM_ForceStop(EPWM, 0x10);
}

void pwm_on_L(void){
    EPWM_Start(EPWM, 0x10);
}

void pwm_off_R(void){
    EPWM_ForceStop(EPWM, 0x08);
}

void pwm_on_R(void){
    EPWM_Start(EPWM, 0x08);
}



uint8_t lenth_det_L(void){
    if(L_GET_BAT_LENTH() == 0){
        return 1;
    }
    else{
        return 0;
    }
}


uint8_t lenth_det_R(void){
    if(R_GET_BAT_LENTH() == 0){
        return 1;
    }
    else {
        return 0;
    }
}


/**
 * @description: 左电池pwm调节
 * @param {uint32_t} duty
 * @return {*}
 */
void regula_pwm_L(uint32_t duty){
    EPWM_SET_CMR(EPWM, 4, duty);
}

/**
 * @description: 右电池pwm调节
 * @param {uint32_t} duty
 * @return {*}
 */
void regula_pwm_R(uint32_t duty){
    EPWM_SET_CMR(EPWM, 3, duty);
}




/**
 * @description: pid调节电流 增量式pid计算
 * @param {pid_t *} pPid
 * @param {uint32_t} target
 * @param {uint32_t} current
 * @return {*}
 */
int32_t Pid_calculate(pid_t * pPid , uint32_t target , uint32_t current){
    int32_t out;
    int32_t err = target - current;

    /*增量式pid计算*/
    out = pPid->Kp * (err - pPid->err1) + pPid->Ki * err + pPid->Kd * (err - 2 * pPid->err1 + pPid->err2);

    /*更新误差*/
    pPid->err2 = pPid->err1;
    pPid->err1 = err;

    /*去格式化*/
    out = R_Q15(out);            

    /*限幅*/
    if(out > pPid->out_max) 
        out = pPid->out_max;
    else if(out < pPid->out_min) 
        out = pPid->out_min;
    
    return out;
}


/**
 * @description: 温度pid
 * @param {pid_t *} pPid
 * @param {uint32_t} target
 * @param {uint32_t} current
 * @return {*}
 */
int32_t Temp_Pid_calculate(pid_t * pPid , uint32_t target , uint32_t current){
    int32_t out;
    int32_t err = current - target;

    /*增量式pid计算*/
    out = pPid->Kp * (err - pPid->err1) + pPid->Ki * err + pPid->Kd * (err - 2 * pPid->err1 + pPid->err2);

    /*更新误差*/
    pPid->err2 = pPid->err1;
    pPid->err1 = err;

    /*去格式化*/
    out = R_Q15(out);            

    /*限幅*/
    if(out > pPid->out_max) 
        out = pPid->out_max;
    else if(out < pPid->out_min) 
        out = pPid->out_min;
    
    return out;
}


/**
 * @description: 滑动平均滤波
 * @param {void *} pData
 * @param {uint32_t} data
 * @param {uint8_t} data_num
 * @return {*}
 */
uint16_t Filter_calculate(uint16_t * pData , uint32_t data , uint8_t data_num){
    uint32_t sum = 0;
    for(uint8_t i = 0; i < data_num - 1; i++){
        pData[i] = pData[i + 1];
        sum += pData[i];
    }
    pData[data_num - 1] = data;
    sum += data;
    return sum / data_num;
}





/**
 * @description: 定时关断充电 20H
 * @param {Battery_t *} pBat
 * @return {*}
 */
void Timed_off_charge(Battery_t * pBat){
    if(++pBat->charge_time >= Charge_off_time){
        pBat->charge_time = 0;
        pBat->charge_off_flag = 1;
    } else {
        pBat->charge_off_flag = 0;
    }
}


/**
 * @description: 预充电结束检测
 * @param {Battery_t} *pBat
 * @return {*}
 */
void PreCharge_end_detect(Battery_t *pBat){
    if(pBat->voltage > Pre_Charge_Voltage){
        pBat->preCharge_end_cnt++;
        if(pBat->preCharge_end_cnt >= Pre_Charge_end_time){
            pBat->preCharge_end_cnt = 0;
            pBat->preCharge_end = 1;
        }
    } else if(pBat->voltage < Pre_Charge_hys_vol){
        if(pBat->preCharge_end_cnt) pBat->preCharge_end_cnt--;
        else{
            pBat->preCharge_end_cnt = 0;
        }
    }
}

/**
 * @description: 激活检测
 * @param {Battery_t *} pBat
 * @return {*}
 */
void Active_detect(Battery_t * pBat){
    if(pBat->voltage >= Low_Power_voltage){
        if(++pBat->active_finish_cnt >= 500){
            pBat->active_finish_cnt = 0;
            pBat->active_flag = 1;
        }
    }
    else{
        if(pBat->active_finish_cnt) pBat->active_finish_cnt--;
        else{
            pBat->active_finish_cnt = 0;
            pBat->active_flag = 0;
        }
    }
}

/**
 * @description: 充电电流档位调节
 * @param {Battery_t} *pBat
 * @return {*}
 */
void Charge_current_set(Battery_t * pBat){
        switch(pBat->current_stage){
            case CURRENT_05A:
                pBat->current_stage = CURRENT_10A;
            break;

            case CURRENT_10A:
                pBat->current_stage = CURRENT_15A;
            break;

            case CURRENT_15A:
                pBat->current_stage = CURRENT_20A;
            break;

            case CURRENT_20A:
                pBat->current_stage = CURRENT_05A;
            break;

            default:
            break;
    }
    pBat->current_Modify = 1;
    // pBat->current_set = (uint8_t)pBat->current_stage * 500 + 500;
}


/**
 * @description: 电池插入检测
 * @param {Battery_t} *pBat
 * @return {*}
 */
// void Battery_insert_detect(Battery_t *pBat){
//     if(pBat->voltage < BATTERY_INSERT_VOLTAGE && pBat->voltage > 500){
//         pBat->pull_out_cnt = 0;
//         if(++pBat->insert_cnt >= BATTERY_INSERT_DETECT_TIME){
//             pBat->insert_cnt = 0;
//             pBat->insert_flag = 1;
//         }
//     }
//     else if(pBat->voltage > BATTERY_PULLOUT_VOLTAGE){
//         pBat->insert_cnt = 0;
//         if(++pBat->pull_out_cnt >= 5){
//             pBat->pull_out_cnt = 0;
//             pBat->insert_flag = 0;
//             if(pBat->err.Bit.Reverse_conect == 1){
//                 pBat->reve_prot_pull_out = 1;
//             }
//             else{
//                 pBat->reve_prot_pull_out = 0;
//             }
//         }
//     }
// }

void Battery_insert_detect(Battery_t *pBat){
    if(pBat->realtime_voltage < BATTERY_INSERT_VOLTAGE && pBat->voltage > 500){
        pBat->pull_out_cnt = 0;
        if(++pBat->insert_cnt >= BATTERY_INSERT_DETECT_TIME){
            pBat->insert_cnt = 0;
            pBat->insert_flag = 1;
        }
    }
    else if(pBat->realtime_voltage > BATTERY_PULLOUT_VOLTAGE){
        pBat->insert_cnt = 0;
        if(++pBat->pull_out_cnt >= 5){
            pBat->pull_out_cnt = 0;
            pBat->insert_flag = 0;
            if(pBat->err.Bit.Reverse_conect == 1){
                pBat->reve_prot_pull_out = 1;
            }
            else{
                pBat->reve_prot_pull_out = 0;
            }
        }
    }
}


/**
 * @description: 电池容量计算
 * @param {Battery_t} *pBat
 * @return {*}
 */
void Capacity_calculate(Battery_t * pBat){
    static uint32_t last_time = 0;
    static uint32_t current_sum = 0;
    static uint8_t sample_cnt = 0;
    uint32_t current_time;
    uint32_t time_diff;
    uint32_t avg_current;
    
    /* 获取当前时间 */
    current_time = Get_system_Timestamp();
    
    /* 如果电池未插入，容量清零并重置采样 */
    if(pBat->insert_flag == 0){
        pBat->capacity = 0;
        last_time = current_time;
        current_sum = 0;
        sample_cnt = 0;
        return;
    }
    
    /* 从预充电阶段开始计算容量 */
    if(pBat->state >= BAT_PreChage){
        /* 累加电流采样值 */
        current_sum += pBat->current;
        sample_cnt++;
        
        /* 每10次采样计算一次平均值 */
        if(sample_cnt >= 10){
            /* 计算时间差(ms) */
            time_diff = current_time - last_time;
            
            /* 计算平均电流 */
            avg_current = current_sum / sample_cnt;
            
            /* 计算容量增量(mAh) = 平均电流(mA) * 时间(h) */
            /* 时间从ms转换为h: time_diff/3600000 */
            pBat->capacity += (avg_current * time_diff) / 3600000;
            
            /* 重置采样计数和累加值 */
            current_sum = 0;
            sample_cnt = 0;
            last_time = current_time;
        }
    }
}



/**
 * @description: 电池电量格数判定
 * @param {Battery_t *} pBat
 * @return {*}
 */
void Bat_Satge_judg(Battery_t * pBat){
    switch(pBat->vol_stage.stage){
        case BAT_STAGE_ZERO:
            if(pBat->voltage >= BAT_STAGE_ONE_VOL){
                pBat->vol_stage.cancel_cnt = 0;
                if(++pBat->vol_stage.sure_cnt >= BAT_STAGE_JUDG_TIME){
                    pBat->vol_stage.sure_cnt = 0;
                    pBat->vol_stage.stage = BAT_STAGE_ONE;
                }
            }
            else if(pBat->voltage <= BAT_STAGE_ONE_LAG_VOL){
                pBat->vol_stage.sure_cnt = 0;
            }
            break;

        case BAT_STAGE_ONE:
            if(pBat->voltage >= BAT_STAGE_TWO_VOL){
                pBat->vol_stage.cancel_cnt = 0;
                if(++pBat->vol_stage.sure_cnt >= BAT_STAGE_JUDG_TIME){
                    pBat->vol_stage.sure_cnt = 0;
                    pBat->vol_stage.stage = BAT_STAGE_TWO;
                }
            }
            else if(pBat->voltage <= BAT_STAGE_ONE_LAG_VOL){
                if(++pBat->vol_stage.cancel_cnt >= BAT_STAGE_JUDG_TIME){
                    pBat->vol_stage.cancel_cnt = 0;
                    pBat->vol_stage.stage = BAT_STAGE_ZERO;
                }
            }
            else if(pBat->voltage <= BAT_STAGE_TWO_LAG_VOL){
                pBat->vol_stage.sure_cnt = 0;
            }

            break;

        case BAT_STAGE_TWO:
            if(pBat->voltage >= BAT_STAGE_THREE_VOL){
                pBat->vol_stage.cancel_cnt = 0;
                if(++pBat->vol_stage.sure_cnt >= BAT_STAGE_JUDG_TIME){
                    pBat->vol_stage.sure_cnt = 0;
                    pBat->vol_stage.stage = BAT_STAGE_THREE;
                }
            }
            else if(pBat->voltage <= BAT_STAGE_TWO_LAG_VOL){
                if(++pBat->vol_stage.cancel_cnt >= BAT_STAGE_JUDG_TIME){
                    pBat->vol_stage.cancel_cnt = 0;
                    pBat->vol_stage.stage = BAT_STAGE_ONE;
                }
            }
            else if(pBat->voltage <= BAT_STAGE_THREE_LAG_VOL){
                pBat->vol_stage.sure_cnt = 0;
            }
            
            break;

        case BAT_STAGE_THREE:
            if(pBat->voltage >= BAT_STAGE_FOUR_VOL){
                pBat->vol_stage.cancel_cnt = 0;
                if(++pBat->vol_stage.sure_cnt >= BAT_STAGE_JUDG_TIME){
                    pBat->vol_stage.sure_cnt = 0;
                    pBat->vol_stage.stage = BAT_STAGE_FOUR;
                }
            }
            else if(pBat->voltage <= BAT_STAGE_THREE_LAG_VOL){
                if(++pBat->vol_stage.cancel_cnt >= BAT_STAGE_JUDG_TIME){
                    pBat->vol_stage.cancel_cnt = 0;
                    pBat->vol_stage.stage = BAT_STAGE_TWO;
                }
            }
            else if(pBat->voltage <= BAT_STAGE_FOUR_LAG_VOL){
                pBat->vol_stage.sure_cnt = 0;
            }
            
            break;

        case BAT_STAGE_FOUR:
            if(pBat->voltage <= BAT_STAGE_FOUR_LAG_VOL){
                pBat->vol_stage.sure_cnt = 0;
                if(++pBat->vol_stage.cancel_cnt >= BAT_STAGE_JUDG_TIME){
                    pBat->vol_stage.cancel_cnt = 0;
                    pBat->vol_stage.stage = BAT_STAGE_THREE;
                }
            }
            break;
            
        default:
            break;
    }
}



/**
 * @description: 电池长度检测
 * @param {Battery_t} *pBat
 * @return {*}
 */
void Battery_lenth_detect(Battery_t *pBat){
    if(pBat->lenth_detect() == 1){
        if(++pBat->lenth_cnt > BATTERY_LENTH_DET_TIME){
            pBat->lenth_cnt = 0;
            pBat->lenth = 1;
        }
    }
    else {
        pBat->lenth_cnt = 0;
        pBat->lenth = 0;
    }
}



/****************************************************** 故障检测 ******************************************************/
/**
 * @description: 预充超时
 * @param {Battery_t *} pBat
 * @return {*}
 */
void PreCharge_timeout_det(Battery_t * pBat){
    if(pBat->preCharge_end == 0){
        if(++pBat->pre_charg_time > Pre_Charge_timeout_max){
            pBat->pre_charg_time = 0;
            pBat->err.Bit.PreC_TimeOut = 1;
        }
    } else {
        pBat->pre_charg_time = 0;
        pBat->err.Bit.PreC_TimeOut = 0;
    }
}


/**
 * @description: 充电输出超压检测
 * @param {Battery_t *} pBat
 * @return {*}
 */
void Vout_over_detect(Battery_t * pBat){
    if(pBat->voltage > Vout_up_limt){
        if(++pBat->ov_cnt > Vout_fault_time){
            pBat->ov_cnt = 0;
            pBat->err.Bit.Over_voltage = 1;
        }
    } 
    else if(pBat->voltage < Vout_low_limt){
        if(pBat->ov_cnt) pBat->ov_cnt--;
        else {
            pBat->ov_cnt = 0;
            pBat->err.Bit.Over_voltage = 0;
        }
    }
}


/**
 * @description: 短路检测
 * @param {Battery_t *} pBat
 * @return {*}
 */
void Short_circuit_detect(Battery_t * pBat){
    if(pBat->adc_current.data >= pBat->short_prot_value){
        if(++pBat->short_cnt > Short_Current_time){
            pBat->short_cnt = 0;
            pBat->err.Bit.Short_circuit = 1;
        }
    } else if(pBat->adc_current.data < (pBat->short_prot_value * 0.8)){
        if(pBat->short_cnt){
            pBat->short_cnt = 0;
        }
    }
}


/**
 * @description: 过流检测
 * @param {Battery_t *} pBat
 * @return {*}
 */
void Over_current_detect(Battery_t * pBat){
    if(pBat->current_Modify){
        pBat->current_Modify = 0;
        pBat->oc_cnt = 0;
        switch (pBat->current_stage)
        {
            case CURRENT_05A:
                pBat->over_current_value = OC_05A_MAX;
                break;
            
            case CURRENT_10A:
                pBat->over_current_value = OC_10A_MAX;
                break;

            case CURRENT_15A:
                pBat->over_current_value = OC_15A_MAX;
                break;

            case CURRENT_20A:
                pBat->over_current_value = OC_20A_MAX;
                break;
            
            default:
                break;
        }
    }
    else{
        if(pBat->current >= pBat->over_current_value){
            if(++pBat->oc_cnt >= Over_Current_time){
                pBat->oc_cnt = 0;
                pBat->err.Bit.OverCurrent = 1;
            }
        }
        else{
            pBat->oc_cnt = 0;
        }
    }
}



/**
 * @description: 反接检测
 * @param {Battery_t *} pBat
 * @return {*}
 */
void Reverse_connect_detect(Battery_t * pBat){
    if(pBat->adc_voltage.data < 50){
        if(++pBat->reverse_cnt >= Reverse_connect_time){
            pBat->reverse_cnt = 0;
            pBat->err.Bit.Reverse_conect = 1;
        }
    } else if(pBat->adc_voltage.data >= 1000){
        if(pBat->reverse_cnt) pBat->reverse_cnt--;
        else{
            pBat->reverse_cnt = 0;
            pBat->err.Bit.Reverse_conect = 0;
        }
    }
}


/**
 * @description: 低压 / 带保护板的电池无法激活 检测
 * @param {Battery_t *} pBat
 * @return {*}
 */
void Low_voltage_detect(Battery_t * pBat){
    if(pBat->voltage < Low_Power_voltage){
        if(++pBat->lv_cnt > Low_Power_time){
            pBat->lv_cnt = 0;
            pBat->err.Bit.Low_power = 1;
        }
    }
    else if(pBat->voltage > Low_Power_hys_voltage){
        if(pBat->lv_cnt) pBat->lv_cnt--;
        else{
            pBat->lv_cnt = 0;
        }
    }
}


/**
 * @description: 清除故障计数
 * @param {Battery_t *} pBat
 * @return {*}
 */
void clear_fault_cnt(Battery_t * pBat){
    pBat->lv_cnt      = 0;
    pBat->oc_cnt      = 0;
    pBat->ov_cnt      = 0;
    pBat->reverse_cnt = 0;
    pBat->short_cnt   = 0;
}


/**
 * @description: 电池左/右状态机
 * @param {Battery_t *} pBat
 * @return {*}
 */
#define DEBUG_NO_PROTECT        1
#define FULL_CNT    500
void Battery_state_machine(Battery_t * pBat){
    switch (pBat->state)
    {
        case BAT_IDLE:
            // pBat->en_charge();
            if(pBat->insert_flag){
                pBat->state = BAT_WAIT_ACTIVAT;
                pBat->fast_show = 1;
            }
            /*反接检测*/
            Reverse_connect_detect(pBat);

            /*反接/温度保护*/
            if(pBat->err.Byte & (BIT3 | BIT6)){
                pBat->dis_charge();
                pBat->pwm_off();
                clear_fault_cnt(pBat);
                pBat->pre_state = BAT_IDLE;
                pBat->state = BAT_Fault;
            }
            break;

        case BAT_WAIT_ACTIVAT:      /*等待电池激活状态*/
            /*低压/激活 检测*/
            Low_voltage_detect(pBat);
            #if DEBUG_NO_PROTECT
            if(pBat->err.Byte){
                pBat->dis_charge();
                clear_fault_cnt(pBat);          /*清除故障计数*/
                pBat->pre_state = BAT_WAIT_ACTIVAT;
                pBat->state = BAT_Fault;        /*跳转故障态*/
            }
            #endif
            /*激活检测*/
            Active_detect(pBat);
            if(pBat->active_flag){
                pBat->active_flag = 0;
                pBat->active_finish_cnt = 0;
                pBat->state = BAT_PreChage;
            }

            if(pBat->insert_flag == 0){
                pBat->dis_charge();
                pBat->active_finish_cnt = 0;
                pBat->state = BAT_IDLE;
            }
            break;  
            
        case BAT_PreChage:
            /*预充结束检测*/
            PreCharge_end_detect(pBat);
            if(pBat->preCharge_end){
                pBat->preCharge_end = 0;
                pBat->preCharge_end_cnt = 0;
                pBat->state = BAT_Charge;
            }

            #if DEBUG_NO_PROTECT
            if(pBat->err.Byte){
                pBat->dis_charge();
                clear_fault_cnt(pBat);
                pBat->pre_state = BAT_PreChage;
                pBat->state = BAT_Fault;
            }
            #endif

            if(pBat->insert_flag == 0){
                pBat->dis_charge();
                pBat->state = BAT_IDLE;
            }
            break;

        case BAT_Charge:
            if(pBat->charge_off_flag){
                pBat->charge_off_flag = 0;
                pBat->dis_charge();
                pBat->state = BAT_Charge_off;
            }

            if(pBat->insert_flag == 0){
                pBat->dis_charge();
                pBat->state = BAT_IDLE;
            }

            #if DEBUG_NO_PROTECT
            if(pBat->err.Byte){
                pBat->dis_charge();
                clear_fault_cnt(pBat);
                pBat->pre_state = BAT_Charge;
                pBat->state = BAT_Fault;
            }

            #endif
            

            if(pBat->current_display_state == NORMAL_CHARGE){
                if(pBat->voltage > 4190){           //pBat->current < 100 && 
                    if(++pBat->current_full_cnt > 3000){
                        pBat->current_full_cnt = 0;
                        pBat->state = BAT_FULL;
                        pBat->dis_charge();
                    }
                }
                else{
                    if(pBat->current_full_cnt)  pBat->current_full_cnt = 0;
                }
            }
            else{
                if(pBat->vol_full_cnt)  pBat->vol_full_cnt = 0;
                if(pBat->current_full_cnt)  pBat->current_full_cnt = 0;
            }

            break;

        case BAT_Charge_off:
            if(pBat->insert_flag == 0){
                pBat->dis_charge();
                pBat->state = BAT_IDLE;
            }
            if(pBat->err.Byte){
                pBat->dis_charge();
                clear_fault_cnt(pBat);
                pBat->pre_state = BAT_Charge_off;
                pBat->state = BAT_Fault;
            }
            break;


        case BAT_FULL:
            if(pBat->vol_full_cnt)  pBat->vol_full_cnt = 0;
            if(pBat->current_full_cnt)  pBat->current_full_cnt = 0;

            if(pBat->insert_flag == 0){
                pBat->dis_charge();
                pBat->state = BAT_IDLE;
            }
            if(pBat->err.Byte){
                pBat->dis_charge();
                clear_fault_cnt(pBat);
                pBat->pre_state = BAT_Charge_off;
                pBat->state = BAT_Fault;
            }
            break;

        case BAT_Fault:
            if(pBat->err.Bit.Reverse_conect){
                if(pBat->reve_prot_pull_out){
                    pBat->err.Bit.Reverse_conect = 0;
                    pBat->dis_charge();
                    pBat->state = BAT_IDLE;
                }
            }
            else{
                if(pBat->pre_state > BAT_IDLE && pBat->pre_state < BAT_Fault){
                    if(pBat->insert_flag == 0){
                        pBat->err.Byte = 0;         /*清除故障*/
                        pBat->dis_charge();
                        pBat->state = BAT_IDLE;     /*跳转空闲态*/
                    }
                    if(pBat->temp_pro_recover && pBat->err.Byte == 0){
                        pBat->temp_pro_recover = 0;
                        pBat->state = BAT_IDLE;//pBat->pre_state;
                    }
                }
                else{
                    if(pBat->temp_pro_recover && pBat->err.Byte == 0){
                        pBat->temp_pro_recover = 0;
                        pBat->state = BAT_IDLE;
                    }
                }
            }
            break;

        default:
            break;
    }
}



/**
 * @description: 电流设定显示进程
 * @param {Battery_t *} pBat
 * @return {*}
 */
void current_set_process(Battery_t * pBat){
    switch (pBat->current_display_state)
    {
    case NORMAL_CHARGE:
        if(pBat->key.long_flag){
            pBat->key.long_flag = 0;
            // pBat->page_SetOrEnd = 1;        /*进入设置电流界面*/
            // gDisplay_refresh = 1;
            // pBat->screen_refresh_time = 0;
            // pBat->current_display_state = SET_CURRENT;
        }
        if(pBat->key.short_flag){
            pBat->key.short_flag = 0;
            pBat->page_SetOrEnd = 1;        /*进入设置电流界面*/
            gDisplay_refresh = 1;
            pBat->screen_refresh_time = 0;
            pBat->current_display_state = SET_CURRENT;
        }

        if(pBat->flash_time_cnt)
            pBat->flash_time_cnt = 0;
        break;
    
    case SET_CURRENT:
        if(pBat->key.short_flag){
            pBat->key.short_flag = 0;
            /*充电状态下调节充电电流*/
            Charge_current_set(pBat);
            gDisplay_refresh = 1;
            pBat->screen_refresh_time = 0;
            pBat->flash_time_cnt = 0;
        }

        if(++pBat->flash_time_cnt > 300){
            pBat->flash_time_cnt = 0;
            pBat->page_SetOrEnd = 1;
            gDisplay_refresh = 1;
            pBat->screen_refresh_time = 0;
            pBat->current_display_state = NORMAL_CHARGE;
        }

        // if(pBat->key.long_flag){
        //     pBat->key.long_flag = 0;
        //     pBat->flash_time_cnt = 0;
        //     pBat->page_SetOrEnd = 1;
        //     gDisplay_refresh = 1;
        //     pBat->screen_refresh_time = 0;
        //     pBat->current_display_state = NORMAL_CHARGE;
        // }
        break;
    
    case SET_CURRENT_END:
        pBat->current_display_state = NORMAL_CHARGE;
        break;
    default:
        break;
    }
}








    
